#include "Runnable.h"


/*---------------------------------------------------------------------------------------
--	FUNCTION:	Runnable::Runnable () : thread(0), threadID(0), running(false)
--
--	DATE:			April 07, 2010
--
--	REVISIONS		(Date and Description):
--
--	DESIGNER:		Eric Ribeiro
--
--	PROGRAMMER:		Eric Ribeiro
--
--	RETURNS:
--
--	NOTES:
--		Function to run a thread
----------------------------------------------------------------------------------------*/
Runnable::Runnable () : thread(0), threadID(0), running(false)
{
}

bool Runnable::startRunning ()
{
    if (running)
        return true;

    thread = CreateThread(0, 0, threadEntryPoint, this, 0, &threadID);

    if (thread != NULL)
        return true;

    return false;
}
/*---------------------------------------------------------------------------------------
--	FUNCTION:	Runnable::isRunning ()
--
--	DATE:			April 07, 2010
--
--	REVISIONS		(Date and Description):
--
--	DESIGNER:		Eric Ribeiro
--
--	PROGRAMMER:		Eric Ribeiro
--
--	RETURNS:
--
--	NOTES:
--		Function to check if the thread is running
----------------------------------------------------------------------------------------*/
bool Runnable::isRunning ()
{
    return running;
}

/*---------------------------------------------------------------------------------------
--	FUNCTION:	Runnable::stopRunning ()
--
--	DATE:			April 07, 2010
--
--	REVISIONS		(Date and Description):
--
--	DESIGNER:		Eric Ribeiro
--
--	PROGRAMMER:		Eric Ribeiro
--
--	RETURNS:
--
--	NOTES:
--		Function to stop the thread
----------------------------------------------------------------------------------------*/
void Runnable::stopRunning ()
{
    CloseHandle(thread);
    running = false;
}

/*---------------------------------------------------------------------------------------
--	FUNCTION:	Runnable::run ()
--
--	DATE:			April 07, 2010
--
--	REVISIONS		(Date and Description):
--
--	DESIGNER:		Eric Ribeiro
--
--	PROGRAMMER:		Eric Ribeiro
--
--	RETURNS:
--
--	NOTES:
--		Function to run the thread
----------------------------------------------------------------------------------------*/
void Runnable::run ()
{
}

/*---------------------------------------------------------------------------------------
--	FUNCTION:	DWORD WINAPI threadEntryPoint (LPVOID args)
--
--	DATE:			April 07, 2010
--
--	REVISIONS		(Date and Description):
--
--	DESIGNER:		Eric Ribeiro
--
--	PROGRAMMER:		Eric Ribeiro
--
--	RETURNS:
--
--	NOTES:
--		Function to set the entry thread point
----------------------------------------------------------------------------------------*/
DWORD WINAPI threadEntryPoint (LPVOID args)
{
    Runnable * runnable = (Runnable *) args;

    runnable->running = true;
    runnable->run();
    runnable->running = false;

    return 0;
}
